﻿//
// XN4SL - Xna Framework for Silverlight.
//
using System;
using System.Collections.ObjectModel;

namespace Xn4Sl.Framework
{
    /// <summary>
    /// A collection of game components.
    /// </summary>
    public sealed class GameComponentCollection : Collection<IGameComponent>
    {
        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        public GameComponentCollection() { }

        /// <summary>
        /// Raised when a component is added to the GameComponentCollection.
        /// </summary>
        public event EventHandler<GameComponentCollectionEventArgs> ComponentAdded;

        /// <summary>
        /// Raised when a component is removed from the GameComponentCollection.
        /// </summary>
        public event EventHandler<GameComponentCollectionEventArgs> ComponentRemoved;

        /// <summary>
        /// Removes all children from the collection.
        /// </summary>
        protected override void ClearItems() 
        {
            this.Clear();
        }

        /// <summary>
        /// Inserts a child object into the collection at the specified location.
        /// </summary>
        /// <param name="index">The position in the collection.</param>
        /// <param name="item">The child object being inserted.</param>
        protected override void InsertItem(int index, IGameComponent item) 
        {
            this.InsertItem(index, item);

            if (this.ComponentAdded != null)
            {
                this.ComponentAdded(this, new GameComponentCollectionEventArgs(item));
            }
        }

        /// <summary>
        /// Removes a child object in the collection.
        /// </summary>
        /// <param name="index">The index of the item being removed.</param>
        protected override void RemoveItem(int index) 
        {
            this.RemoveAt(index);

            if (this.ComponentRemoved != null)
            {
                this.ComponentRemoved(this, null);
            }
        }

        /// <summary>
        /// Modifies the specified child object in the collection.
        /// </summary>
        /// <param name="index">The position in the collection.</param>
        /// <param name="item">The child object being modified.</param>
        protected override void SetItem(int index, IGameComponent item) 
        {
            this[index] = item;
        }
    }
}

